//
//  DetailsBedView.swift
//  AirTravel
//
//  Created by apple on 2021/7/22.
//

import UIKit
import JXBanner
import JXPageControl
import FWPopupView

protocol DetailsBedDelegate:NSObjectProtocol {
    func detailsBedClick(data:Dictionary<String,Any>)
}

class DetailsBedView: FWPopupView {
    
    
    var model:CityHotelDetailModel?
    
    
    var img = ""
    var contentView:UIView!
    weak var delegate: DetailsBedDelegate?
    let identifyXB = "XnBannerCell"
    let identifyRI = "RoomInformationCell"
    let identifyEP = "ExpensesPolicyCell"
    let identifyHH = "HotelHeaderView"
    let identifyCP = "CurrentPoliticsCell"
    
    
    /// 编号
    @IBOutlet weak var serialLab: UILabel!
    @IBOutlet weak var titleLab: UILabel!
    
    @IBOutlet weak var reservationBtn: UIButton!
    @IBOutlet weak var priceLab: UILabel!
    @IBOutlet weak var tabView: UITableView!
    
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
        
        self.tabView.dataSource = self
        self.tabView.delegate = self
        self.tabView.estimatedRowHeight = 150
        self.tabView.rowHeight = UITableView.automaticDimension
        self.tabView.tableFooterView = UIView.init()
        self.tabView.register(UINib.init(nibName: "XnBannerCell", bundle: nil), forCellReuseIdentifier: identifyXB)
        self.tabView.register(UINib.init(nibName: "RoomInformationCell", bundle: nil), forCellReuseIdentifier: identifyRI)
        self.tabView.register(UINib.init(nibName: "ExpensesPolicyCell", bundle: nil), forCellReuseIdentifier: identifyEP)
        self.tabView.register(UINib.init(nibName: "HotelHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: identifyHH)
        self.tabView.register(UINib.init(nibName: "CurrentPoliticsCell", bundle: nil), forCellReuseIdentifier: identifyCP)
    }
    
    
    
    
    
    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    
    lazy var banner: JXBanner = {
        let banner = JXBanner()
        banner.placeholderImgView.image = UIImage(named: "banner_placeholder")
        banner.delegate = self
        banner.tag = 0
        banner.dataSource = self
        banner.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 217)
        return banner
    }()
    
    @IBAction func backBtn(_ sender: UIButton) {
        self.hide()
        self.hideKeyboard()
    }
    
    
    
    @IBAction func gotoPay(_ sender: UIButton) {
        
        let vc = BookingHotelController()
        
        let s = currentVc()
        s?.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    func setData(){
        tabView.reloadData()
    }
}

extension DetailsBedView:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int{
        return 4
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        self.titleLab.text = model?.baseRoomInfo?.roomName
        
     
        self.serialLab.text = "编号:\(model?.roomInfo?.first?.roomID ?? "")"
        
        self.priceLab.text = "￥\(model?.minprice ?? "")"
        

        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: identifyXB) as! XnBannerCell
            cell.selectionStyle = .none
            cell.backView.addSubview(self.banner)
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: identifyRI) as! RoomInformationCell
            
            cell.areaLab.text = model?.roomInfo?.first?.areaRange
            cell.bedTypeLab.text = "\(model?.baseRoomInfo?.bedTypeInfo?.first?.bedCount ?? "")张\(model?.baseRoomInfo?.bedTypeInfo?.first?.bedName ?? "")\(model?.baseRoomInfo?.bedTypeInfo?.first?.bedWidth ?? "")米"
            cell.canLiveLab.text = "\(model?.roomInfo?.first?.person ?? "")人"
            
            if(model?.roomInfo?.first?.hasWindow == "-1"){
                cell.haveWindowLab.text = "无窗"
            }else{
                cell.haveWindowLab.text = "有窗"
            }
            
            if(model?.roomInfo?.first?.addBedFee == "-2"){
                cell.extraBedLab.text = "不能加床"
            }else{
                cell.extraBedLab.text = "可加床"
            }
            
            if(model?.baseRoomInfo?.smokeInfo?.notAllowSmoking == nil){
                cell.extraBedLab.text = "可吸烟"
            }else{
                cell.extraBedLab.text = "不可吸烟"
            }
            
            if(model?.roomInfo?.first?.roomPriceInfo?.roomPriceDetail?.first?.breakfast == "0" ){
                cell.breakfastLab.text = "无早餐"
            }else{
                cell.breakfastLab.text = "有早餐"
            }
            
            cell.selectionStyle = .none
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: identifyEP) as! ExpensesPolicyCell
            
            cell.contentLab.text = model?.roomInfo?.first?.promotionInfo?.text
            cell.selectionStyle = .none
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: identifyCP) as! CurrentPoliticsCell
            cell.contenLab.text = model?.baseRoomInfo?.serviceNames
            cell.selectionStyle = .none
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        switch indexPath.section {
        case 0:
            return 217
        default:
            return UITableView.automaticDimension
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        switch section {
        case 3:
            return 50
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
        switch section {
        case 2:
            return 8
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
        let headView = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifyHH) as! HotelHeaderView
        switch section {
        case 3:
            headView.titleLab.text = "房型政策"
            headView.img.isHidden = true
            headView.leftLine.constant = 18
        default:
            headView.img.isHidden = true
            headView.titleLab.isHidden = true
        }
        return headView
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?{
        let footerView = UIView.init()
        footerView.backgroundColor = UIColor.groupTableViewBackground
        return footerView
    }
}

extension DetailsBedView:JXBannerDataSource,JXBannerDelegate{
    
    func jxBanner(numberOfItems banner: JXBannerType) -> Int {
        return 3
    }
    
    func jxBanner(_ banner: JXBannerType) -> (JXBannerCellRegister) {
        return JXBannerCellRegister(type: nil,
                                    reuseIdentifier: "HomeBannerColl",
                                    nib: UINib(nibName: "HomeBannerColl", bundle: Bundle.main))
    }
    
    func jxBanner(_ banner: JXBannerType, cellForItemAt index: Int, cell: UICollectionViewCell) -> UICollectionViewCell {
        if let tempCell = cell as? HomeBannerColl {
//            if let array = self.banners {
//                let dic = array[index] as! Dictionary<String,Any>
//                tempCell.img.kf.setImage(with: URL.init(string: "\(dic["img"] ?? "")"))
//
//            }
            
            tempCell.img.kf.setImage(with: URL.init(string: "\(img)"))
//            tempCell.img.image = UIImage.init(named: "")
        }
        return cell
    }
    
    func jxBanner(_ banner: JXBannerType,
                  layoutParams: JXBannerLayoutParams)
        -> JXBannerLayoutParams {
        return layoutParams
            .itemSize(CGSize(width: SCREEN_WIDTH, height: 217))
    }
    
    func jxBanner(pageControl banner: JXBannerType, numberOfPages: Int, coverView: UIView, builder: JXBannerPageControlBuilder) -> JXBannerPageControlBuilder {
        let pageControl = JXPageControlScale()
        pageControl.contentMode = .bottomRight
        pageControl.activeSize = CGSize(width: 15, height: 6)
        pageControl.inactiveSize = CGSize(width: 6, height: 6)
        pageControl.activeColor = UIColor.white
        pageControl.inactiveColor = UIColor.white
        pageControl.columnSpacing = 0
        pageControl.isAnimation = true
        builder.pageControl = pageControl
        builder.layout = {
            pageControl.snp.makeConstraints { (maker) in
                maker.left.right.equalTo(coverView)
                maker.top.equalTo(coverView.snp.bottom).offset(-10)
                maker.height.equalTo(20)
            }
        }
        return builder
    }
    
    func jxBanner(_ banner: JXBannerType, params: JXBannerParams) -> JXBannerParams {
        return params.timeInterval(3)
    }
    
    func jxBanner(_ banner: JXBannerType,
                      didSelectItemAt index: Int) {
        
    }

}
